package com.example.androidproject.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

import com.example.androidproject.app.MainActivity;

public abstract class BaseFragment extends Fragment {
    private final String TAG = "GGSTUDY";
    public static Context mContext;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return initView();
    }

    public abstract View initView();

    /**
     * onActivityCreated 的 替代方案
     * onAttach()始终在任何Lifecycle 状态更改之前调用，所以onAttach()在onCreate()之前调用。
     *
     * @param context
     */
//    @Override
//    public void onAttach(@NonNull Context context) {
//        super.onAttach(context);
//        //requireActivity() 返回的是宿主activity
//        requireActivity().getLifecycle().addObserver(new LifecycleEventObserver() {
//            @Override
//            public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
//                if (event.getTargetState() == Lifecycle.State.CREATED) {
//                    initData();
//                    Log.d("GGSTUDY", "onStateChanged: 123");
//                    getLifecycle().removeObserver(this);  //这里是删除观察者
//                }
//            }
//        });
//    }

    /**
     * onActivityCreated 的 替代方案
     * onAttach()始终在任何Lifecycle 状态更改之前调用，所以onAttach()在onCreate()之前调用。
     */
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();

    }

    public void initData() {
    }

    public void setNavClickListener(TextView tv_search, TextView tv_login) {
        tv_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MainActivity.class);
                /**
                 * Intent.FLAG_ACTIVITY_CLEAR_TOP：销毁目标Activity和它之上的所有Activity，重新创建目标Activity，不会调用onNewIntent方法。
                 * Intent.FLAG_ACTIVITY_SINGLE_TOP ：当该activity处于栈顶时，可以复用，直接onNewIntent。
                 */
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("position", 3);
                startActivity(intent);
//                Toast.makeText(mContext, "登录", Toast.LENGTH_SHORT).show();
            }
        });
        tv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClick: " + "搜索");
                Toast.makeText(mContext, "搜索", Toast.LENGTH_SHORT).show();
            }
        });
    }


    /**
     * 此方法已经弃用
     */
//    @Override
//    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
//        super.onActivityCreated(savedInstanceState);
//        initData();
//    }

}
